package libs.yzx.zaza.acts3;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import libs.yzx.zaza.R;
import libs.yzx.zaza.util.CanvasTestView;

/**
 * Created by Administrator on 2016/10/19
 */
public class CanvasPaintAcrtivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CanvasTestView view = new CanvasTestView(this);
        view.setBackgroundColor(Color.WHITE);
        setContentView(view);

    }


    @Override
    public void onBackPressed() {
        Bitmap source = BitmapFactory.decodeResource(getResources(), R.mipmap.tu);
        Bitmap result = clipBitmap(source, new Rect(source.getWidth()/3, source.getHeight()/3, source.getWidth()/3*2, source.getHeight()/3*2));
        ImageView iv = new ImageView(this);
        iv.setImageBitmap(result);
        setContentView(iv);
    }

    /* 裁剪bitmap... */
    private Bitmap clipBitmap(Bitmap source, Rect rect){
        Bitmap result = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(source,-rect.left,-rect.top,null);
        return result;
    }

}
